//
//  AccountBindingSVC.m
//  SelfDriving
//
//  Created by apple on 16/1/11.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AccountBindingSVC.h"
#import "BusinessService+User.h"
#import "FriendsListVC.h"
#import "AddressBookFVC.h"
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
#import <AddressBook/AddressBook.h>

@interface AccountBindingSVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (weak, nonatomic) IBOutlet UIButton *btn;

@end

@implementation AccountBindingSVC
{
    int count;
    
    NSMutableString *phone;
    NSMutableArray *phonedata;
}

- (void)xhw2018205add {
    phonedata = [[NSMutableArray alloc] init]; //xhw2018-2-9
    
    int __block tip=0;
    //声明一个通讯簿的引用
    ABAddressBookRef addBook =nil;
    //因为在IOS6.0之后和之前的权限申请方式有所差别，这里做个判断
    if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) {
        //创建通讯簿的引用
        addBook=ABAddressBookCreateWithOptions(NULL, NULL);
        //创建一个出事信号量为0的信号
        dispatch_semaphore_t sema=dispatch_semaphore_create(0);
        //申请访问权限
        ABAddressBookRequestAccessWithCompletion(addBook, ^(bool greanted, CFErrorRef error)        {
            //greanted为YES是表示用户允许，否则为不允许
            if (!greanted) {
                tip=1;
            }
            //发送一次信号
            dispatch_semaphore_signal(sema);
        });
        //等待信号触发
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }else{
        //IOS6之前
        addBook =ABAddressBookCreate();
    }
    if (tip) {
        //做一个友好的提示
        UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您设置允许APP访问您的通讯录-设置>隐私" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alart show];
    }
    //获取所有联系人的数组
    CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook);
    //获取联系人总数
    CFIndex number = ABAddressBookGetPersonCount(addBook);

    //进行遍历
    for (NSInteger i=0; i<number; i++) {
        //获取联系人对象的引用
        ABRecordRef  people = CFArrayGetValueAtIndex(allLinkPeople, i);
        //获取当前联系人名字
        NSString*firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
        firstName = firstName?firstName:@"";
        //获取当前联系人姓氏
        NSString*lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
        lastName = lastName?lastName:@"";
        
        //获取当前联系人头像图片
        NSData*userImage=(__bridge NSData*)(ABPersonCopyImageData(people));
        
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        //读取电话多值
        ABMultiValueRef phone = ABRecordCopyValue(people, kABPersonPhoneProperty);
        for (int k = 0; k<ABMultiValueGetCount(phone); k++)
        {
            //获取电话Label
            NSString * personPhoneLabel = (NSString*)CFBridgingRelease(ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k)));
            //获取該Label下的电话值
            NSString * personPhone = (NSString*)CFBridgingRelease(ABMultiValueCopyValueAtIndex(phone, k));
            personPhone = [personPhone stringByReplacingOccurrencesOfString:@"-" withString:@""];
            personPhone = [personPhone stringByReplacingOccurrencesOfString:@"*" withString:@""];
            personPhone = [personPhone stringByReplacingOccurrencesOfString:@"+86" withString:@""];
            personPhone = [personPhone stringByReplacingOccurrencesOfString:@";" withString:@""];
            personPhone = [personPhone stringByReplacingOccurrencesOfString:@" " withString:@""];
            [dic setObject:[NSString stringWithFormat:@"%@",[personPhone trim]] forKey:@"phone"];
        }
        [dic setObject:[NSString stringWithFormat:@"%@%@",lastName,firstName] forKey:@"name"];
        [dic setObject:userImage?userImage:@"" forKey:@"img"];
        [phonedata addObject:dic];

    }
    
    phone = [[NSMutableString alloc]init];
    for (int i= 0; i<phonedata.count; i++) {
        NSMutableDictionary *dic = [phonedata objectAtIndex:i];
        if ([dic objectForKey:@"phone"] == nil) {
            continue;
        }
        [phone appendFormat:@"%@,",[dic objectForKey:@"phone"]];
    }

    
    if ([phone isEqualToString:@""]) {
        return;
    }else {
        phone = [phone substringToIndex:phone.length-1];
    }
    

    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    count = 2;
    [self xhw2018205add];
    

    self.navigationItem.title = [NSString stringWithFormat:@"添加%@好友",_type];
    NSLog(@"xhw->通讯录是否绑定->%@",[AppDelegate shareDelegate].txl);
    if ([_type isEqualToString:@"通讯录"]) {
        if([[AppDelegate shareDelegate].txl isEqualToString:@"绑定"]){
            count = 2;
            [_btn setTitle:@"关闭通讯录" forState:UIControlStateNormal];
        }else{
            count = 1;
            [_btn setTitle:@"开启通讯录" forState:UIControlStateNormal];
        }
    }else if([_type isEqualToString:@"新浪"]){
        if ([[AppDelegate shareDelegate].user.bind_sina isEqualToString:@"1"]) {
            [_btn setTitle:@"解除绑定" forState:UIControlStateNormal];
        }
        else {
            [_btn setTitle:@"绑定" forState:UIControlStateNormal];
        }
    }else if ([_type isEqualToString:@"QQ"]){
        if ([[AppDelegate shareDelegate].user.bind_qq isEqualToString:@"1"]) {
            [_btn setTitle:@"解除绑定" forState:UIControlStateNormal];
        }
        else {
            [_btn setTitle:@"绑定" forState:UIControlStateNormal];
        }
        
    }else if ([_type isEqualToString:@"微信"]){
        if ([[AppDelegate shareDelegate].user.bind_weixin isEqualToString:@"1"]) {
            [_btn setTitle:@"解除绑定" forState:UIControlStateNormal];
        }
        else {
            [_btn setTitle:@"绑定" forState:UIControlStateNormal];
        }
        
    }
    

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSLog(@"xhwCount->%d",count);
    return count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    if(section == 1){
        if([_type isEqualToString:@"新浪"]){
            if ([[AppDelegate shareDelegate].user.bind_sina isEqualToString:@"1"]) {
                return 1;
            }
            else {
                return 0;
            }
        }else if ([_type isEqualToString:@"QQ"]){
            if ([[AppDelegate shareDelegate].user.bind_qq isEqualToString:@"1"]) {
                return 1;
            }
            else {
                return 0;
            }
        } else if ([_type isEqualToString:@"微信"]){
            if ([[AppDelegate shareDelegate].user.bind_weixin isEqualToString:@"1"]) {
                return 1;
            }
            else {
                return 0;
            }
        }
        //xhw2018-2-5加
        else if ([_type isEqualToString:@"通讯录"]) {
            return 1;
        }
        //xhw2018-2-5加
        return 0;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 300;
    }else{
        return 45;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if([_type isEqualToString:@"新浪"]){

            BOOL isOauth = YES;
            
            if ([[AppDelegate shareDelegate].user.bind_sina isEqualToString:@"1"]) {
                isOauth = YES;
            }
            else {
                isOauth = NO;
            }
            
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(SCRE_WIDTH/2-212/2, 30, 212, 150)];
            img.image = [UIImage imageNamed:@"bangdingzu"];
            [cell.contentView addSubview:img];
            
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 230, SCRE_WIDTH, 30)];
            lable.textAlignment = NSTextAlignmentCenter;
            lable.text = isOauth?@"已绑定新浪微博":@"未绑定新浪微博";
            [cell.contentView addSubview:lable];
            
            UILabel *desc = [[UILabel alloc]initWithFrame:CGRectMake(0, 260, SCRE_WIDTH, 30)];
            desc.textAlignment = NSTextAlignmentCenter;
            desc.textColor = [UIColor grayColor];
            desc.text = isOauth?@"系统为你推荐新浪微博好友":@"";
            [cell.contentView addSubview:desc];
        }
//        else if ([_type isEqualToString:@"腾讯"]){
//            snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
//            BOOL isOauth = [UMSocialAccountManager isOauthAndTokenNotExpired:snsPlatform.platformName];
//            
//            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(SCRE_WIDTH/2-212/2, 30, 212, 150)];
//            img.image = [UIImage imageNamed:@"bangdingzu"];
//            [cell.contentView addSubview:img];
//            
//            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 230, SCRE_WIDTH, 30)];
//            lable.textAlignment = NSTextAlignmentCenter;
//            lable.text = isOauth?@"已绑定腾讯微博":@"未绑定新腾讯微博";
//            [cell.contentView addSubview:lable];
//            
//            UILabel *desc = [[UILabel alloc]initWithFrame:CGRectMake(0, 260, SCRE_WIDTH, 30)];
//            desc.textAlignment = NSTextAlignmentCenter;
//            desc.textColor = [UIColor grayColor];
//            desc.text = isOauth?@"系统为你推荐腾讯微博好友":@"";
//            [cell.contentView addSubview:desc];
        else if ([_type isEqualToString:@"QQ"]){

            BOOL isOauth = YES;
            
            if ([[AppDelegate shareDelegate].user.bind_qq isEqualToString:@"1"]) {
                isOauth = YES;
            }
            else {
                isOauth = NO;
            }
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(SCRE_WIDTH/2-212/2, 30, 212, 150)];
            img.image = [UIImage imageNamed:@"bangdingzu"];
            [cell.contentView addSubview:img];
            
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 230, SCRE_WIDTH, 30)];
            lable.textAlignment = NSTextAlignmentCenter;
            lable.text = isOauth?@"已绑定QQ":@"未绑定QQ";
            [cell.contentView addSubview:lable];
            
            UILabel *desc = [[UILabel alloc]initWithFrame:CGRectMake(0, 260, SCRE_WIDTH, 30)];
            desc.textAlignment = NSTextAlignmentCenter;
            desc.textColor = [UIColor grayColor];
            desc.text = isOauth?@"系统为你推荐QQ好友":@"";
            [cell.contentView addSubview:desc];
        }else if ([_type isEqualToString:@"微信"]){

            BOOL isOauth = YES;
            
            if ([[AppDelegate shareDelegate].user.bind_weixin isEqualToString:@"1"]) {
                isOauth = YES;
            }
            else {
                isOauth = NO;
            }
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(SCRE_WIDTH/2-212/2, 30, 212, 150)];
            img.image = [UIImage imageNamed:@"bangdingzu"];
            [cell.contentView addSubview:img];
            
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 230, SCRE_WIDTH, 30)];
            lable.textAlignment = NSTextAlignmentCenter;
            lable.text = isOauth?@"已绑定微信":@"未绑定微信";
            [cell.contentView addSubview:lable];
            
            UILabel *desc = [[UILabel alloc]initWithFrame:CGRectMake(0, 260, SCRE_WIDTH, 30)];
            desc.textAlignment = NSTextAlignmentCenter;
            desc.textColor = [UIColor grayColor];
            desc.text = isOauth?@"系统为你微信好友":@"";
            [cell.contentView addSubview:desc];
        }else{
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(SCRE_WIDTH/2-212/2, 30, 212, 150)];
            img.image = [UIImage imageNamed:@"bangdingzu"];
            [cell.contentView addSubview:img];
            
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(SCRE_WIDTH/2-90/2, 80, 80, 90)];
            lable.textAlignment = NSTextAlignmentCenter;
            lable.numberOfLines = 0;
            lable.font = [UIFont systemFontOfSize:12];
            lable.textColor = [UIColor grayColor];
//            lable.text = count == 2?[AppDelegate shareDelegate].user.phone:@"通讯录仅用于查询好友，不会泄露隐私";
            [cell.contentView addSubview:lable];
        }
        
        return  cell;
    }
    if (indexPath.section == 1) {
        NSLog(@"xhw添加好友是否进入");
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell2"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = @"添加好友";
        return cell;
    }
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([_type isEqualToString:@"新浪"]){
        if (indexPath.section==1) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            FriendsListVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendsListVC"];
            searchResultDetailController.type = @"3";
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
    }else if ([_type isEqualToString:@"QQ"]){
        if (indexPath.section==1) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            FriendsListVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendsListVC"];
            searchResultDetailController.type = @"1";
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
    }else{
        if (indexPath.section==1) {
//             UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
//                                                                         bundle:[NSBundle mainBundle]];
//            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"MailListViewController"];
//            [self.navigationController pushViewController:searchResultDetailController animated:YES];
            
            //xhw2018-2-9加
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                         bundle:[NSBundle mainBundle]];
            AddressBookFVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"AddressBookFVC"];

            searchResultDetailController.phone = phone;
            searchResultDetailController.ary = phonedata;
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
    }
}
- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 关闭通讯录/解除绑定
- (IBAction)closeAction:(UIButton *)sender {
    if([_type isEqualToString:@"新浪"]){
        if ([sender.titleLabel.text isEqualToString:@"解除绑定"]) {
        
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            [dic setObject:@"3" forKey:@"type"];
            [dic setObject:@"0" forKey:@"optype"];
            [[BusinessService shareInstance]bindAccount:dic success:^(id result) {
                [UserDefaults clearUserDefaultWithKey:@"sinaUsid"];
                count = 1;
                [_btn setTitle:@"绑定" forState:UIControlStateNormal];
                [SVProgressHUD showSuccessWithStatus:@"解绑成功"];

                [[BusinessService shareInstance] getUserDetail:dic success:^(id result) {
                    [AppDelegate shareDelegate].user = result;
                    [_table reloadData];
                } failure:^(id result) {
                    NSLog(@"失败");
                }]; //xhw2018-1-25加
                [_table reloadData];
            } failure:^(id result) {
                [SVProgressHUD showErrorWithStatus:result];
            }];
    
        }else if ([sender.titleLabel.text isEqualToString:@"绑定"]){
            
            [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:nil completion:^(id result, NSError *error) {
                if (error) {
                    
                }else {
                    UMSocialUserInfoResponse *resp = result;
                    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                    [dic setObject:@"3" forKey:@"type"];
                    [dic setObject:@"1" forKey:@"optype"];
                    [dic setObject:resp.accessToken forKey:@"account"];
                    
                    [[BusinessService shareInstance]bindAccount:dic success:^(id result) {
                        [UserDefaults writeUserDefaultObjectValue:resp.accessToken withKey:@"sinaUsid"];
                        count = 2;
                        [_btn setTitle:@"解除绑定" forState:UIControlStateNormal];
                        [_table reloadData];
                        [SVProgressHUD showSuccessWithStatus:@"绑定成功"];

                        [[BusinessService shareInstance] getUserDetail:dic success:^(id result) {
                            [AppDelegate shareDelegate].user = result;
                            [_table reloadData];
                        } failure:^(id result) {
                            NSLog(@"失败");
                        }];
                    } failure:^(id result) {
                        [SVProgressHUD showErrorWithStatus:result];
                    }];
                }
            }];
            /*
            [self bangding:@"sina" back:^(id result) {
                UMSocialAccountEntity *snsAccount = result;
                NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                [dic setObject:@"3" forKey:@"type"];
                [dic setObject:snsAccount.usid forKey:@"account"];
                [dic setObject:@"1" forKey:@"optype"];
                [[BusinessService shareInstance]bindAccount:dic success:^(id result) {
                    [UserDefaults writeUserDefaultObjectValue:snsAccount.usid withKey:@"sinaUsid"];
                    count = 2;
                    [_btn setTitle:@"解除绑定" forState:UIControlStateNormal];
                    [_table reloadData];
                } failure:^(id result) {
                    [SVProgressHUD showErrorWithStatus:result];
                }];
                
            }];
             */
        }
    }else if ([_type isEqualToString:@"QQ"]){
        if ([sender.titleLabel.text isEqualToString:@"解除绑定"]) {

            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            [dic setObject:@"1" forKey:@"type"];
            [dic setObject:@"0" forKey:@"optype"];
            [[BusinessService shareInstance]bindAccount:dic success:^(id result) {
                count = 1;
                [UserDefaults clearUserDefaultWithKey:@"tencentUsid"];
                [SVProgressHUD showSuccessWithStatus:@"解绑成功"];
                [_btn setTitle:@"绑定" forState:UIControlStateNormal];
                [[BusinessService shareInstance] getUserDetail:dic success:^(id result) {
                    [AppDelegate shareDelegate].user = result;
                    [_table reloadData];
                } failure:^(id result) {
                    NSLog(@"失败");
                }]; //xhw2018-1-25加
                [_table reloadData];
            } failure:^(id result) {
                [SVProgressHUD showErrorWithStatus:result];
            }];

            
        }else if ([sender.titleLabel.text isEqualToString:@"绑定"]){
            [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {
                if (error) {
                    
                }else {
                    UMSocialUserInfoResponse *resp = result;
                    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                    [dic setObject:@"1" forKey:@"type"];
                    [dic setObject:@"1" forKey:@"optype"];
                    [dic setObject:resp.openid forKey:@"account"];
                    [[BusinessService shareInstance]bindAccount:dic success:^(id result) {
                        [UserDefaults writeUserDefaultObjectValue:resp.openid withKey:@"tencentUsid"];
                        count = 2;
                        [_btn setTitle:@"解除绑定" forState:UIControlStateNormal];
                        [SVProgressHUD showSuccessWithStatus:@"绑定成功"];
                        [[BusinessService shareInstance] getUserDetail:dic success:^(id result) {
                            [AppDelegate shareDelegate].user = result;
                            [_table reloadData];
                        } failure:^(id result) {
                            NSLog(@"失败");
                        }]; //xhw2018-1-25加
                        [_table reloadData];
                    } failure:^(id result) {
                        [SVProgressHUD showErrorWithStatus:result];
                    }];
                }
            }];
            
            /*
            [self bangding:@"tencent" back:^(id result) {
                UMSocialAccountEntity *snsAccount = result;
                NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                [dic setObject:@"1" forKey:@"type"];
                [dic setObject:snsAccount.usid forKey:@"account"];
                [dic setObject:@"1" forKey:@"optype"];
                [[BusinessService shareInstance]bindAccount:dic success:^(id result) {
                    [UserDefaults writeUserDefaultObjectValue:snsAccount.usid withKey:@"tencentUsid"];
                    count = 2;
                    [_btn setTitle:@"解除绑定" forState:UIControlStateNormal];
                    [_table reloadData];
                } failure:^(id result) {
                    [SVProgressHUD showErrorWithStatus:result];
                }];
                
            }];
             */
        }
    }
    else if ([_type isEqualToString:@"微信"]){
        if ([sender.titleLabel.text isEqualToString:@"解除绑定"]) {

            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            [dic setObject:@"2" forKey:@"type"];
            [dic setObject:@"0" forKey:@"optype"];
            [[BusinessService shareInstance]bindAccount:dic success:^(id result) {
                count = 1;
                [UserDefaults clearUserDefaultWithKey:@"weiXinId"];
                [_btn setTitle:@"绑定" forState:UIControlStateNormal];
                [SVProgressHUD showSuccessWithStatus:@"解绑成功"];

                [[BusinessService shareInstance] getUserDetail:dic success:^(id result) {
                    [AppDelegate shareDelegate].user = result;
                    [_table reloadData];
                } failure:^(id result) {
                    NSLog(@"失败");
                }]; //xhw2018-1-25加
                [_table reloadData];
            } failure:^(id result) {
                [SVProgressHUD showErrorWithStatus:result];
            }];

            
        }else if ([sender.titleLabel.text isEqualToString:@"绑定"]){
            
            [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
                if (error) {
                    
                }else {
                    UMSocialUserInfoResponse *resp = result;
                    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                    [dic setObject:@"2" forKey:@"type"];
                    [dic setObject:@"1" forKey:@"optype"];
                    [dic setObject:resp.openid forKey:@"account"];
                    [[BusinessService shareInstance]bindAccount:dic success:^(id result) {
                        [UserDefaults writeUserDefaultObjectValue:resp.openid withKey:@"weiXinId"];
                        count = 2;
                        [_btn setTitle:@"解除绑定" forState:UIControlStateNormal];
                        [SVProgressHUD showSuccessWithStatus:@"绑定成功"];

                        [[BusinessService shareInstance] getUserDetail:dic success:^(id result) {
                            [AppDelegate shareDelegate].user = result;
                        } failure:^(id result) {
                            NSLog(@"失败");
                        }]; //xhw2018-1-25加
                        [_table reloadData];
                    } failure:^(id result) {
                        [SVProgressHUD showErrorWithStatus:result];
                    }];
                }
            }];
//            [self bangding:@"tencent" back:^(id result) {
//                UMSocialAccountEntity *snsAccount = result;
//                NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
//                [dic setObject:@"1" forKey:@"type"];
//                [dic setObject:snsAccount.usid forKey:@"account"];
//                [dic setObject:@"1" forKey:@"optype"];
//                [[BusinessService shareInstance]bindAccount:dic success:^(id result) {
//                    [UserDefaults writeUserDefaultObjectValue:snsAccount.usid withKey:@"tencentUsid"];
//                    count = 2;
//                    [_btn setTitle:@"解除绑定" forState:UIControlStateNormal];
//                    [_table reloadData];
//                } failure:^(id result) {
//                    [SVProgressHUD showErrorWithStatus:result];
//                }];
//
//            }];
        }
    }
    else if ([_type isEqualToString:@"通讯录"]){
        if ([sender.titleLabel.text isEqualToString:@"关闭通讯录"]) {
            count = 1;
            [AppDelegate shareDelegate].txl = @"未绑定";
            [_btn setTitle:@"开启通讯录" forState:UIControlStateNormal];
            [_table reloadData];
        }else if ([sender.titleLabel.text isEqualToString:@"开启通讯录"]){
            count = 2;
            [AppDelegate shareDelegate].txl = @"绑定";
            [_btn setTitle:@"关闭通讯录" forState:UIControlStateNormal];
            [_table reloadData];
        }
    }
}
-(void)bangding:(NSString *)name back:(void(^)(id result))back{
    /*
    NSString *platformName;
    if([name isEqualToString:@"sina"]){
        platformName = UMShareToSina;
    }else if ([name isEqualToString:@"tencent"]){
        platformName = UMShareToQQ;
        
    }
    [UMSocialControllerService defaultControllerService].socialUIDelegate = self;
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:platformName];
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        //获取微博用户名、uid、token、第三方的原始用户信息thirdPlatformUserProfile等
        if (response.responseCode == UMSResponseCodeSuccess) {
            NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:platformName];
            back(snsAccount);
        }else{
            [SVProgressHUD showErrorWithStatus:response.error];
        }
        
    });
     */
}
-(void)jcbangding:(NSString *)name back:(void(^)(id result))back{
    /*
    NSString *platformName;
    if([name isEqualToString:@"sina"]){
        platformName = UMShareToSina;
    }else if ([name isEqualToString:@"tencent"]){
        platformName = UMShareToQQ;
        
    }else if([name isEqualToString:@"weixin"]){
        platformName = UMShareToWechatSession;
    }
    [[UMSocialDataService defaultDataService] requestUnOauthWithType:platformName completion:^(UMSocialResponseEntity *response) {
        NSLog(@"unOauth response is %@",response);
        back(response);
    }];
     */
}


@end
